/**
 * @author jspider
 * @description WebsocketClient Constructor
 * @param url
 */
var WebsocketClient = function(url) {
	this.url = url;
	this.socket = null;
	this.highcharObj = [];
}

/**
 * @author jspider
 * @description WebsocketWebClient
 */
WebsocketClient.prototype = {
	/**
	 * @description create the connect function
	 * @param {Object}
	 *            host
	 */
	connect: function(host) {
		var that = this;
		if ('WebSocket' in window) {
			that.socket = new WebSocket(host);
		} else if ('MozWebSocket' in window) {
			that.socket = new MozWebSocket(host);
		} else {
			console.log('Error: WebSocket is not supported by this browser.');
			return;
		}

		that.socket.onopen = function() {
			console.log('Info: WebSocket connection opened.');
			/*
			 * document.getElementById('chat').onkeydown = function(event) { if
			 * (event.keyCode == 13) { Chat.sendMessage(); } };
			 */
			/*for (var index = 0; index < 100; index++) {
				that.sendMessage("I am " + index);
				that.sendMessage("shuchudianyaUbc")
			}*/
			var signal_options = {
				"plantID": 1,
				"signalTypes": []
			};
			that.sendMessage(JSON.stringify(signal_options));
		};

		that.socket.onclose = function() {
			/* document.getElementById('chat').onkeydown = null; */
			console.log('Info: WebSocket closed.');
		};

		that.socket.onmessage = function(message) {
			console.log(message.data);
			//console.log(message.data);
			var signal_str = message.data;

			if (signal_str) {

				var signal_json = JSON.parse(signal_str);
				var signal_value = "",
					signal_desc = "",
					signal_unit = "";

				$.each(signal_json, function(key, value) {
					//console.log(value);
					if (value['speed']) {
						var content = value['speed'];
					/*	console.log(content);*/
						signal_value = content['signalValue'];
						signal_unit = content['symbolUnit'];
						$("span:first", ("#speed_signal div.signal-value")).text(signal_value);
						$("span:eq(1)", ("#speed_signal div.signal-value")).text(signal_unit);
					} 
					if (value['power']) {
						//console.log(value['power']);
						var content = value['power'];
						signal_value = content['signalValue'];
						signal_unit = content['symbolUnit'];
						$("span:first", ("#power_signal div.signal-value")).text(signal_value);
						$("span:eq(1)", ("#power_signal div.signal-value")).text(signal_unit);
					} 
					if (value['errorcount']) {
						var content = value['errorcount'];
						signal_value = content['signalValue'];
						signal_unit = content['symbolUnit'];

						$("span:first", ("#error_signal div.signal-value")).text(signal_value);
						$("span:eq(1)", ("#error_signal div.signal-value")).text(signal_unit);
					} 
					if (value['mainstatus']) {
						$.each(value['mainstatus'], function(key, content) {
							signal_value = content['signalValue'];
							signal_unit = content['symbolUnit'];
							signal_desc = content['signalDec'];
							if (signal_value == 'true') {
								signal_value = "ON";
							} else if (signal_value == 'false') {
								signal_value = "OFF";
							}
							$("span:first", (".signal-value[data-signalName='" + signal_desc + "']")).text(signal_value);
						});
					}
				});

			}
		};
	},

	/**
	 * @desc initialize the websocketclient object
	 */
	initialize: function() {

		var that = this;
		/*setInterval(function() {

			var x = (new Date()).getTime(), // current time
			y = Math.random();
			var series = that.highcharObj.series[0]
			series.addPoint([ x, y ], true, true);
		}, 1000);*/
		/*if (window.location.protocol == 'http:') {
			that.connect('ws://' + window.location.host
					+ '/websocket/websocket/chat');
		} else {
			that.connect('wss://' + window.location.host
					+ '/websocket/websocket/chat');
		}*/
		that.connect("ws://10.12.18.33:8080/wsTest/websocketServer")
			//that.connect("ws://127.0.0.1:8080/websocket/websocket/chat")
	},

	/**
	 * @desc send message given to the server
	 * @param {Object}
	 *            message
	 */
	sendMessage: function(message) {
		var that = this;
		that.socket.send(message);
	}
}